package com.gmics.unifiq.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.gmics.unifiq.entity.Group;
import com.gmics.unifiq.entity.Member;

public class GroupController {

	// JSON parser class
	JSONParser jsonParser = new JSONParser();

	private JSONController jController = new JSONController();
	private static String retrieve_groups_url = "http://unifiq.5kportal.com/debbieunifiq/retrieve_groups.php";

	// url to update product
	private static final String url_update_icon = "http://unifiq.5kportal.com/debbieunifiq/update_group_icon.php";
	private static String url_get_members = "http://unifiq.5kportal.com/debbieunifiq/get_members.php";

	private static final String TAG_SUCCESS = "success", TAG_GROUP = "group",
			TAG_GROUPID = "groupID", TAG_NAME = "name",
			TAG_DESCRIPTION = "description", TAG_IMAGEPATH = "imagePath",
			TAG_MEMBER = "member",
			TAG_USERID = "userID", TAG_EMAIL = "email",
			TAG_PROFILEPIC = "profilePic";

	public ArrayList<Group> retrieveGroups() {
		ArrayList<Group> groupsList = new ArrayList<Group>();
		JSONArray groupJArr = null;

		List<NameValuePair> params = new ArrayList<NameValuePair>();
		JSONObject json = jController.makeHttpRequest(retrieve_groups_url,
				"GET", params);

		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				groupJArr = json.getJSONArray(TAG_GROUP);

				for (int i = 0; i < groupJArr.length(); i++) {
					JSONObject jObject = groupJArr.getJSONObject(i);

					int groupID = jObject.getInt(TAG_GROUPID);
					String name = jObject.getString(TAG_NAME);
					String description = jObject.getString(TAG_DESCRIPTION);
					String imagePath = jObject.getString(TAG_IMAGEPATH);

					Group gp = new Group();

					gp.setGroupID(groupID);
					gp.setDescription(description);
					gp.setImagePath(imagePath);
					gp.setName(name);

					groupsList.add(gp);

				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return groupsList;
	}

	public void updateGroupIcon(int groupID, String imagename) {

		String url = "http://unifiq.5kportal.com/images/post_images/"
				+ imagename;

		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(TAG_IMAGEPATH, url));
		params.add(new BasicNameValuePair(TAG_GROUPID, String.valueOf(groupID)));

		// sending modified data through http request
		// Notice that update product url accepts POST method
		JSONObject json = jsonParser.makeHttpRequest(url_update_icon, "POST",
				params);

		// check json success tag
		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				// successfully updated
			} else {
				// failed to update product
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<Member> getMembers(int groupID) {
		
		ArrayList<Member> memberList = new ArrayList<Member>();
		JSONArray MemberJArr = null;
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add((new BasicNameValuePair(TAG_GROUPID, String.valueOf(groupID) )));
		JSONObject json2 = jController.makeHttpRequest(url_get_members, "GET",
				params);

		try {
			int success = json2.getInt(TAG_SUCCESS);

			if (success == 1) {
				MemberJArr = json2.getJSONArray(TAG_MEMBER);

				for (int i = 0; i < MemberJArr.length(); i++) {
					JSONObject jObject2 = MemberJArr.getJSONObject(i);

					int userID = jObject2.getInt(TAG_USERID);
					String name = jObject2.getString(TAG_NAME);
					String email = jObject2.getString(TAG_EMAIL);
					String profilePic = jObject2.getString(TAG_PROFILEPIC);
					
					Member Member = new Member();

					Member.setUserID(userID);
					Member.setEmail(email);
					Member.setGroupID(groupID);
					Member.setName(name);
					Member.setProfilePic(profilePic);
					
					memberList.add(Member);
				}
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return memberList;
	}

}
